/*
 * UnitFabric.cpp
 *
 *  Created on: 10.12.2011
 *      Author: Kirill Blinov
 *      Copyright: LGPL
 */

#include "UnitFabric.h"
#include "CStaticUnit.h"
#include "CDynamicUnit.h"

/*
 * This function adds a new template with specified name and "GameObject" parameters
 *
 * Return value:
 *   true - if a template successfully added
 *   false - unit can't be added ( a unit with TemplateName is already exist )
 */
bool
UnitFabric::AddTemplate( const UNIT_TYPES UnitType, const std::string TemplateName, GAME_OBJECT* GameObject )
{
	IUnit* NewTemplate = NULL;

	switch ( UnitType )
	{
		break;case UNIT_TYPE_HEAVY_JET:
		{
			NewTemplate = new CHeavyJet( GameObject );
		}
		break;case UNIT_TYPE_MEDIUM_JET:
		{
			NewTemplate = new CMediumJet( GameObject );
		}
		break;case UNIT_TYPE_LIGHT_JET:
		{
			NewTemplate = new CLightJet( GameObject );
		}
		break;case UNIT_TYPE_RACKET_:
		{
			NewTemplate = new CRacket( GameObject );
		}
		break;case UNIT_TYPE_MILK:
		{
			NewTemplate = new CMilk( GameObject );
		}
		break;case UNIT_TYPE_BULLET:
		{
			NewTemplate = new CBullet( GameObject );
		}
		break;case UNIT_TYPE_BONUS:
		{
			NewTemplate = new CBonus( GameObject );
		}
		break;case UNIT_TYPE_SCENARY:
		{
			NewTemplate = new CScenary( GameObject );
		}
		break;
	}

	bool Result = false;

	if ( NewTemplate != NULL )
	{
		Result = UnitFabric::Templates.AddTemplate( NewTemplate, TemplateName );
	}

	return Result;
}

/*
 * This function fabricates a new unit of "UnitType" with name of "Name".
 * Pointer on a Unit returns through "NewUnit" parameter, it may be NULL.
 *
 * Return value:
 * 	true - if a Unit created successfully
 *  false - if requested Unit can't be created
 */
bool
UnitFabric::CreateUnit( const UNIT_PHYSIC_PARAMS &PhysicParams, const std::string TemplateName, const std::string UnitName,
		    			IUnit** NewUnit )
{
	IUnit* CreatedUnit = NULL;

	bool Result = true;
	//TODO Add checking for unique unit name and add it to a resource manager

	CreatedUnit = UnitFabric::Templates.CreateUnitInstance( PhysicParams, TemplateName, UnitName );

	if ( ( NewUnit != NULL ) &&  ( Result ) )
	{
		*NewUnit = CreatedUnit;
	}

	return ( CreatedUnit != NULL );
}

/*
 * Deletes a unit with specified name.
 *
 * Return value:
 *  false - if a Unit with name "Name" isn't exist
 */
bool
UnitFabric::DeleteUnit( const std::string Name )
{
	//TODO Fill the function with resource manager interaction
	return true;
}

/*
 * This function uses when resources like vertex buffer have been
 * reloaded for cleaning up templates list and all created units
 */
void
UnitFabric::CleanUnits()
{
	//TODO Add resource manager units clean
	UnitFabric::Templates.Clear();
}

CTemplateCollection UnitFabric::Templates;

